////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Software       :                                                         //
//   Layer          : SRC                                                     //
//   Package        :                                                         //
//   Component      :                                                         //
//                                                                            //
//   File           : cxml.cxx                                                //
//   Author         : RSO ( Rafael Sotomayor Brûlé )                          //
//   EMail          : rsotomayor@savtec.cl                                    //
//   Type           : C++ Source                                              //
//   Usage          :                                                         //
//   Purpose        : gestión de usuario                                      //
//                                                                            //
//                                                                            //
//   who when        what                                                     //
//   ~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//   RSO 09-10-2010  Creation                                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////


#include <sys/time.h> 

#include "cxml.h"

using namespace std;

CXML::CXML() {


} ;

CXML::~CXML() {

}

int CXML::parseResult ( string buffer_p, string &response_p ) {

  xmlDocPtr doc;
  xmlNodePtr cur;
  int retcode = -1 ;
  doc  = xmlParseMemory(buffer_p.data(),buffer_p.length());

  if ( doc == NULL ) {   
    errLog::LOGERR((errLog::WARN,(char *)"Document not parsed successfully. \n" )); 
    return -1;
  }   
  errLog::LOGERR((errLog::TRACE,"Parsing Successful\n")); 

  cur = xmlDocGetRootElement ( doc );

  if ( cur == NULL ) {   
    errLog::LOGERR((errLog::WARN,"empty document \n" )); 
    xmlFreeDoc ( doc );
    return -1;
  }   
  errLog::LOGERR((errLog::TRACE,"Got the root Node\n")); 
  if ( xmlStrcmp ( cur->name, ( const xmlChar * ) "result" ) ) {
    errLog::LOGERR((errLog::WARN,"Document of the wrong type root node != \n")); 
    xmlFreeDoc(doc);
    return -1;
  }
  errLog::LOGERR((errLog::TRACE,"Got the root \n")); 

  xmlChar *key;

  cur = cur -> xmlChildrenNode;
  while ( cur != NULL ) {
    if ( !(xmlStrcmp ( cur->name, ( const xmlChar * ) "response" ) ) ) {
      key = xmlNodeListGetString ( doc, cur -> xmlChildrenNode,1);
      response_p = (char *) key ;
      xmlFree ( key );
    }
    cur = cur -> next;
  }
  xmlFreeDoc ( doc );
  return retcode ;
}

int CXML::parseBuffer ( string buffer_p,  string &response_p ) {

  xmlDocPtr doc;
  xmlNodePtr cur;
  int retcode = -1 ;
  doc  = xmlParseMemory(buffer_p.data(),buffer_p.length());

  if ( doc == NULL ) {   
    errLog::LOGERR((errLog::WARN,"Document not parsed successfully. \n" )); 
    return -1;
  }   
  errLog::LOGERR((errLog::TRACE,"Parsing Successful\n")); 

  cur = xmlDocGetRootElement ( doc );

  if ( cur == NULL ) {   
    errLog::LOGERR((errLog::WARN,"empty document \n" )); 
    xmlFreeDoc ( doc );
    return -1;
  }   
  errLog::LOGERR((errLog::TRACE,"Got the root Node\n")); 
  if ( xmlStrcmp ( cur->name, ( const xmlChar * ) "data" ) ) {
    errLog::LOGERR((errLog::WARN,"Document of the wrong type root node != \n")); 
    xmlFreeDoc(doc);
    return -1;
  }
  errLog::LOGERR((errLog::INFO,"Got the root \n")); 

  cur = cur -> xmlChildrenNode;
  while ( cur != NULL ) {
    if ( !(xmlStrcmp ( cur->name, ( const xmlChar * ) "transaccion" ) ) ) {
      retcode = parseTransaccion( doc, cur,response_p );
    }
    cur = cur -> next;
  }
  xmlFreeDoc ( doc );
  return retcode ;
}

int CXML::parseTransaccion ( xmlDocPtr doc, xmlNodePtr cur,  string &response_p  ) {
  xmlChar *key;
  int retcode = -1 ;
  int counter=0;
  cur = cur -> xmlChildrenNode;
  while ( cur != NULL ) {   
    if ( ( !xmlStrcmp ( cur -> name, ( const xmlChar * ) "response" ) ) )  {   
      key = xmlNodeListGetString ( doc, cur -> xmlChildrenNode,1);
      response_p = (char *) key ;
      xmlFree ( key );
      counter++;
    } 

    cur = cur -> next;
  }
  if ( counter > 1 ) {
    retcode = 0 ;
  }
  return retcode ;
}


